package com.ljq.rpc.codec;

import com.alibaba.fastjson.JSONObject;
import com.ljq.rpc.common.pojo.Request;
import com.ljq.rpc.common.protocol.dubboProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.CharsetUtil;
import org.springframework.stereotype.Component;

import java.util.List;

public class JSONDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        int len = in.readInt();
        byte[] bytes = new byte[len];
        in.readBytes(bytes);
        String json = new String(bytes, CharsetUtil.UTF_8);
        dubboProtocol msg = JSONObject.parseObject(json, dubboProtocol.class);
        out.add(msg);
    }

}
